package com.surprise.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.surprise.common.annotation.LoginValidate;
import com.surprise.common.bean.Result;
import com.surprise.common.util.StringTool;
import com.surprise.service.SpUserService;

/**
 * 基于用户信息操作的控制层
 * 
 * @author zhangceven
 *
 */
@RestController
public class UserController {

	@Autowired
	SpUserService userService;

	/**
	 * 用户登陆操作
	 * @param request
	 * @param response
	 * @return
	 */
	@LoginValidate
	@RequestMapping(value = "/api/login", method = RequestMethod.POST)
	public Result userLogin(HttpServletRequest request, HttpServletResponse response) {
		// 获取参数
		String phone = request.getParameter("phone"); // 手机号码
		String password = request.getParameter("password"); // 密码
		String serialCode = request.getParameter("serial_code"); // 设备标识码
		String pid = request.getParameter("pid");

		String wechat_openid = request.getParameter("wechat_openid"); // 微信 openid
		String qq_openid = request.getParameter("qq_openid"); // QQ openid
		String open_code = request.getParameter("open_code"); // open_code
		String device = request.getParameter("device"); // 设备类型： ios, Android

		// 处理参数
		serialCode = serialCode == null ? "web" : serialCode;
		int referee = StringTool.LetterToNum(pid);

		Result result = userService.getLogin(phone, password, serialCode, referee, wechat_openid, qq_openid, open_code, device);

		return result;
	}
}
